#!/bin/bash

[ "$1" == "" ] && echo "Usage: `basename $0` file_list" && exit 1

function format_name()
{
  echo "$1" | awk '{\
    gsub("^\[", "", $0);\
    gsub("\(720[pP]\)", "", $0);\
    gsub("\(1080[pP]\)", "", $0);\
    gsub("[\-\[]*FHD[\]\-]*", "", $0);\
    gsub("[\-\[]+HD[\]\-]+", "", $0);\
    gsub("\(www\.javhit\.com\)", "", $0);\
    gsub("\[javtorrent\.biz\]", "", $0);\
    gsub("[Jj]av\.li", "", $0);\
    gsub("javset\.com", "", $0);\
    gsub("3xplanet", "", $0);\
    gsub("www.youiv.net", "", $0);\
    gsub("24jav.net", "", $0);\
    gsub("JAVFILE\.ORG", "", $0);\
    gsub("beancom", "", $0);\
    gsub("heyzo_hd_", "HEYZO-", $0);\
    gsub("^HEY-", "HEYZO-", $0);\
    gsub("^[kK]irari *", "MKD-S");\
    \
    gsub("^[\#\-\_0-9]+", "", $0);\
    pos = match($0, "[0-9][0-9]+");\
    if (pos > 0){\
      p1 = substr($0, 1, pos-1);\
      p2 = substr($0, pos, length($0)-pos+1);\
      pos2 = match(p2, "[a-zA-Z]+");\
      if (pos2 > 0){\
        s1 = substr(p2, 1, pos2-1);\
        s2 = substr(p2, pos2, length(p2)-pos2+1);\
        if (s1 !~ /\.$/) {\
          p2 = s1"-"s2; }\
      }\
      $0 = p1"-"p2;
    }\
    gsub("--", "-", $0); gsub("--", "-", $0);\
    \
    gsub(",", "", $0);\
    gsub("^[ |	]+", "", $0);\
    gsub("[ |	]+$", "", $0);\
    gsub("\[[ |	]+\]", "", $0);\
    gsub("\]", "] ", $0);\
    gsub("\] +", "] ", $0);\
    print toupper($0);\
  }' | awk -F"[-._      ]" 'BEGIN{ SFX="" }{\
    if ( $0 ~ /\./ && length($NF) == 3 ) {\
      SFX = $NF;\
    }\
    if ( $1 ~ /CARIB/ ){\
      printf("%s-%s-%s", $1,$2,$3); \
    } else {\
      printf("%s-%s", $1,$2); \
    }\
    if (length(SFX) > 0){\
      printf(".%s", SFX);\
    }\
    printf("\n");\
  }'
}

function get_info(){
  vid="$1"
  htfn="./.tmp/$vid"
  [ ! -d "./.tmp" ] && mkdir -p ./.tmp
  export http_proxy="http://web-proxy.cce.hp.com:8088"
  [ ! -f "$htfn" ] && wget http://javfree.me/$vid -O $htfn 1>/dev/null 2>&1
  cat $htfn | grep "Published:" | grep "Posted in:" | grep "Tags:" |\
  sed 's/>/>\n/g' | sed 's/</\n</g' | awk '\
  BEGIN{ print_next = 0; tl=0; vid="'"$vid"'"}\
  ( print_next == 1 ) { \
      gsub("\[HD\] *", "", $0);\
      gsub("\[DVD\] *", "", $0);\
      tl=1; \
      if ( $0 !~ /^\[.*/ ) {\
        gsub(/^.*[0-9]+\-[0-9]+/,"["vid"]",$0);\
      }\
      printf("%s ", $0); \
      print_next=0 }\
  ( print_next == 2 ) { \
      printf("{%s}", $0); \
      print_next=0 }\
  ($0 ~ /rel=\"bookmark\"\>$/){ print_next = 1 }\
  ($0 ~ /title=\"[0-9]+/){\
      idx=index($0, /[0-9]{4}\-[0-9]{2}\-[0-9]{2}/);\
      printf("(%s) ", substr($0, idx-1, 10)) }\
  ($0 ~ /rel=\"tag\"\>/){ print_next = 2 }\
  END{ if ( tl == 0 ) { \
      printf("[%s]\n", vid) \
    } else { \
      printf("\n") } \
  }' | awk -F"[{}]" '{gsub("\] ", "] "$(NF-1)" - ", $1); print $1}'
}

function query_one()
{
  list="./`basename $0`_list.bat"
  touch $list

  bas=`echo "$1" | awk -F"." '{print $1}'`
  ext=`echo "$1" | awk -F"." '(NF > 1){print "."$NF}'`
  fmt_nm=`format_name "$bas"`
  echo "Getting $fmt_nm info ......"

  info=`get_info "$fmt_nm"`

  grep "$fmt_nm" "$list"
  [ $? -eq 0 ] && return
  echo "RENAME" "\"$1\"" "\"$info$ext\"" >> $list

  echo "Done"
}

cat "$1" | while read fn
do
  query_one "$fn"
done

